package cn.zcib.dao;

import cn.zcib.domain.Student;

import java.util.ArrayList;

/**
 * @Author Lucky赵
 * @Date 2021年08月11日 9:14
 */
public class OtherStudentDao implements BaseStudentDao {
    private static final ArrayList<Student> students = new ArrayList<>();//创建学生对象数组

    static {
        Student stu1 = new Student("001", "张三", 23, "1998-02-28");
        Student stu2 = new Student("002", "李四", 22, "1999-03-12");
        Student stu3 = new Student("003", "王五", 20, "2001-05-20");
        students.add(stu1);
        students.add(stu2);
        students.add(stu3);
    }

    public boolean addStu(Student student) {
        students.add(student);
        return true;
    }

    public Student[] findAllStu() {
        Student[] stus = new Student[students.size()];
        for (int i = 0; i < stus.length; i++) {
            stus[i]=students.get(i);
        }
        return stus;
    }

    public void delStuById(String id) {
        //查找id在容器中的索引位置
        int index = getIndex(id);
        //将该索引位置使用null元素进行覆盖
        students.remove(index);
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student stu = students.get(i);
            if (stu != null && stu.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStu(String id, Student student) {
        int index = getIndex(id);
        students.set(index, student);
    }
}
